var express = require('express');
var router = express.Router();
var common = require('../common');
var test_server = require('../test_server');
//获取总在线活动人数
router.get('/', function(req, res, next) {
	console.dir(res.cookie);
	var size = common.getRoomCount(global.sockets);
    res.send(common.objectToJson({roomCount:size}));
});

router.get('/test', function(req, res, next) {
  var id = req.query.id;
  var size = common.arraySize(test_server.sockets);
  res.send(common.objectToJson({roomCount:size}));
});
//通过邀请码获取指定房间信息
router.get('/findRoomByCode', function(req, res, next) {
	var code = req.query.code;
	common.initPool();
	var pool = common.getPool();
	var sql = "select * from answer_room WHERE code=? LIMIT 1";
	pool.getConnection(function (err, conn) {
		if (err) console.log("POOL ==> " + err);
		var pids = [];
		conn.query(sql, [code],function(error, results) {
			if(error) {
				console.log("findRoomByCode Error: " + error.message);
				return;
			}
			if(results.length > 0) {
				var room = results[0];
				var id = room['id'];
				res.send(common.objectToJson({id:room['id'],type:room['type'],number:room['number'],status:room['status'],name:room['name']}));
			} else {
				res.send(common.objectToJson({id:-1,type:-1,number:-1,status:-1}));
			}
		});
		conn.release();
	});
});

//获取指定房间的用户数
router.get('/count', function(req, res, next) {
  var id = req.query.id;
  var size = 0;
  if(typeof(global.rooms[id]) != "undefined") {
	  size = common.arraySize(global.rooms[id]);
  }
  res.send(common.objectToJson({roomNumber:size}));
});
//获取指定房间的用户成绩列表
router.get('/list', function(req, res, next) {
  var id=req.query.id;
  if(typeof(global.rooms[id]) != "undefined") {
	  var list = global.rooms[id];
	  var players = [];
	  for (var i in list) {
		  var socketId = list[i];
		  if(typeof(global.connectionList[socketId]) == "undefined") {
			  continue;
		  }
		  players.push({uid:global.connectionList[socketId].uid,socketId:socketId,name:global.connectionList[socketId].name,point:global.connectionList[socketId].point,head:global.connectionList[socketId].head,roomIndex:global.connectionList[socketId].roomIndex,ready:global.connectionList[socketId].ready});
	  }
	  res.send(common.objectToJson(players));
  }
  else res.send("[]");
});
//获取已开始比赛的房间的参赛选手列表
router.get('/players', function(req, res, next) {
  var id=req.query.id;
  if(typeof(global.games[id]) != "undefined") {
	  var game = global.games[id];
	  var players = [];
	  for (var i in game.points) {
		  var name = i;
		  players.push(name);
	  }
	  res.send(common.objectToJson(players));
  }
  else res.send("[]");
});
//获取正在匹配天梯的选手
router.get('/searchingPlayers', function(req, res, next) {
  if((global.searchingPlayers.length) > 0) {
	  res.send(common.objectToJson(global.searchingPlayers));
  }
  else res.send("[]");
});
//获取当下天梯对战的所有房间
router.get('/battles', function(req, res, next) {
  if(common.arraySize(global.battles) > 0) {
	  var clone = {};
	  for(var i in global.battles) {
		var battle = global.battles[i];
		clone[i] = {status:battle.status,players:battle.players,currentIndex:battle.currentIndex,answers:battle.answers,ready:battle.ready,seconds:battle.seconds};
	  }
	  res.send(common.objectToJson(clone));
  }
  else res.send("[]");
});
//获取登录了天梯系统的玩家
router.get('/ladders', function(req, res, next) {
  if(common.arraySize(global.ladders) > 0) {
	  res.send(common.objectToJson(global.ladders));
  }
  else res.send("[]");
});
//获取登录了天梯系统的玩家已答数据
router.get('/playerData', function(req, res, next) {
  if(common.arraySize(global.playerData) > 0) {
	  res.send(common.objectToJson(global.playerData));
  }
  else res.send("[]");
});
//获取登录了天梯系统的玩家数
router.get('/laddersCount', function(req, res, next) {
	var size = common.arraySize(global.ladders);
	if(size > 0) res.send(common.objectToJson({count:size}));
	else res.send(common.objectToJson({count:0}));
});
//获取今日动态
router.get('/getNews', function(req, res, next) {
	var str = common.getNews();
    res.send(str);
});
module.exports = router;
